package com.ericsson.resourceplanner.utils;

import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
@Component
public class ExcelLayoutManager {
 
 private static Logger logger = LoggerFactory.getLogger(ExcelLayoutManager.class);
  
 /**
  * Builds the report title and the date header
  *
  * @param worksheet
  * @param startRowIndex starting row offset
  * @param startColIndex starting column offset
  */
 public static void buildTitle(Sheet worksheet, int startRowIndex, int startColIndex) {
  // Create font style for the report title
  Font fontTitle = worksheet.getWorkbook().createFont();
  fontTitle.setBoldweight(Font.BOLDWEIGHT_BOLD);
  fontTitle.setFontHeight((short) 280);
   
        // Create cell style for the report title
        CellStyle cellStyleTitle = worksheet.getWorkbook().createCellStyle();
        cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER);
        cellStyleTitle.setWrapText(true);
        cellStyleTitle.setFont(fontTitle);
        cellStyleTitle.setBorderBottom(HSSFCellStyle.BORDER_THICK);
        cellStyleTitle.setBorderTop(HSSFCellStyle.BORDER_THICK);
        cellStyleTitle.setBorderRight(HSSFCellStyle.BORDER_THICK);
        cellStyleTitle.setBorderLeft(HSSFCellStyle.BORDER_THICK);
        // Create report title
  Row rowTitle = worksheet.createRow((short) startRowIndex);
  rowTitle.setHeight((short) 500);
  Cell cellTitle = rowTitle.createCell(startColIndex);
  
  cellTitle.setCellStyle(cellStyleTitle);
   
  // Create merged region for the report title
  worksheet.addMergedRegion(new CellRangeAddress(0,0,0,5));
   
  // Create date header
  Row dateTitle = worksheet.createRow((short) startRowIndex +1);
  Cell cellDate = dateTitle.createCell(startColIndex);
  cellDate.setCellValue("This report was generated at " + new Date());
 }
  
 /**
  * Builds the column headers
  *
  * @param worksheet
  * @param startRowIndex starting row offset
  * @param startColIndex starting column offset
  */
 public static CellStyle buildHeaderStyle(Sheet worksheet) {
  // Create font style for the headers
  Font font = worksheet.getWorkbook().createFont();
  font.setBoldweight(Font.BOLDWEIGHT_BOLD);
 
        // Create cell style for the headers
  CellStyle headerCellStyle = worksheet.getWorkbook().createCellStyle();
  headerCellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
  headerCellStyle.setFillPattern(CellStyle.FINE_DOTS);
  headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
  headerCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  headerCellStyle.setFont(font);
  headerCellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
  headerCellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  headerCellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  headerCellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); 
  return headerCellStyle;
 }
 
 
 public static CellStyle buildDataCellStyle(Sheet worksheet) {
	  
	  CellStyle cellStyle = worksheet.getWorkbook().createCellStyle();
	  cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
	  cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	  cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
	  cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	  cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
	  cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
	  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
	  return cellStyle;
	 }
}
